package client

import (
	"context"
	"fmt"
	"google.golang.org/grpc"
	"yk-travel-social/common/proto/travel"
)

type Handler func(ctx context.Context, in travel.TravelClient) (interface{}, error)

func TravelClient(ctx context.Context, handler Handler) (interface{}, error) {
	// 1.连接
	conn, err := grpc.Dial("127.0.0.1:8081", grpc.WithInsecure())
	if err != nil {
		fmt.Printf("连接异常： %s\n", err)
	}
	defer conn.Close()

	client := travel.NewTravelClient(conn)
	res, err := handler(ctx, client)
	if err != nil {
		return nil, err
	}
	return res, err
}

func Login(ctx context.Context, i *travel.LoginRequest) (*travel.LoginResponse, error) {
	client, err := TravelClient(ctx, func(ctx context.Context, in travel.TravelClient) (interface{}, error) {
		login, err := in.Login(ctx, i)
		if err != nil {
			return nil, err
		}
		return login, nil
	})
	if err != nil {
		return nil, err
	}
	return client.(*travel.LoginResponse), err
}

func Search(ctx context.Context, i *travel.SearchRequest) (*travel.SearchResponse, error) {
	client, err := TravelClient(ctx, func(ctx context.Context, in travel.TravelClient) (interface{}, error) {
		login, err := in.Search(ctx, i)
		if err != nil {
			return nil, err
		}
		return login, nil
	})
	if err != nil {
		return nil, err
	}
	return client.(*travel.SearchResponse), err
}

func ShowInfo(ctx context.Context, i *travel.ShowInfoRequest) (*travel.ShowInfoResponse, error) {
	client, err := TravelClient(ctx, func(ctx context.Context, in travel.TravelClient) (interface{}, error) {
		login, err := in.ShowInfo(ctx, i)
		if err != nil {
			return nil, err
		}
		return login, nil
	})
	if err != nil {
		return nil, err
	}
	return client.(*travel.ShowInfoResponse), err
}

func BuyTicket(ctx context.Context, i *travel.BuyTicketRequest) (*travel.BuyTicketResponse, error) {
	client, err := TravelClient(ctx, func(ctx context.Context, in travel.TravelClient) (interface{}, error) {
		login, err := in.BuyTicket(ctx, i)
		if err != nil {
			return nil, err
		}
		return login, nil
	})
	if err != nil {
		return nil, err
	}
	return client.(*travel.BuyTicketResponse), err
}

func AddPurchaser(ctx context.Context, i *travel.AddPurchaserRequest) (*travel.AddPurchaserResponse, error) {
	client, err := TravelClient(ctx, func(ctx context.Context, in travel.TravelClient) (interface{}, error) {
		login, err := in.AddPurchaser(ctx, i)
		if err != nil {
			return nil, err
		}
		return login, nil
	})
	if err != nil {
		return nil, err
	}
	return client.(*travel.AddPurchaserResponse), err
}

func AlipayBack(ctx context.Context, i *travel.AlipayBackRequest) (*travel.AlipayBackResponse, error) {
	client, err := TravelClient(ctx, func(ctx context.Context, in travel.TravelClient) (interface{}, error) {
		login, err := in.AlipayBack(ctx, i)
		if err != nil {
			return nil, err
		}
		return login, nil
	})
	if err != nil {
		return nil, err
	}
	return client.(*travel.AlipayBackResponse), err
}

func ShowOrder(ctx context.Context, i *travel.ShowOrderRequest) (*travel.ShowOrderResponse, error) {
	client, err := TravelClient(ctx, func(ctx context.Context, in travel.TravelClient) (interface{}, error) {
		login, err := in.ShowOrder(ctx, i)
		if err != nil {
			return nil, err
		}
		return login, nil
	})
	if err != nil {
		return nil, err
	}
	return client.(*travel.ShowOrderResponse), err
}
